home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / BTFIRST.C < prev    next >
Text File  |  1991-09-23  |  2KB  |  98 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)btfirst.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10.  
  11. /* library headers */
  12. #include <blkio.h>
  13.  
  14. /* local headers */
  15. #include "btree_.h"
  16.  
  17. /*man---------------------------------------------------------------------------
  18. NAME
  19.      btfirst - first btree key
  20.  
  21. SYNOPSIS
  22.      #include <btree.h>
  23.  
  24.      int btfirst(btp)
  25.      btree_t *btp;
  26.  
  27. DESCRIPTION
  28.      The btfirst function positions the cursor of btree btp on the
  29.      first key in that btree.
  30.  
  31.      btfirst will fail if one or more of the following is true.
  32.  
  33.      [EINVAL]       btp is not a valid btree pointer.
  34.      [BTELOCK]      btp is not locked.
  35.      [BTENKEY]      btp is empty.
  36.      [BTENOPEN]     btp is not open.
  37.  
  38. SEE ALSO
  39.      btkeycnt, btlast, btnext, btprev.
  40.  
  41. DIAGNOSTICS
  42.      Upon successful completion, a value of 0 is returned.  Otherwise,
  43.      a value of -1 is returned, and errno set to indicate the error.
  44.  
  45. ------------------------------------------------------------------------------*/
  46. #ifdef AC_PROTO
  47. int btfirst(btree_t *btp)
  48. #else
  49. int btfirst(btp)
  50. btree_t *btp;
  51. #endif
  52. {
  53.     int terrno = 0;        /* tmp errno */
  54.  
  55.     /* validate arguments */
  56.     if (!bt_valid(btp)) {
  57.         errno = EINVAL;
  58.         return -1;
  59.     }
  60.  
  61.     /* check if not open */
  62.     if (!(btp->flags & BTOPEN)) {
  63.         errno = BTENOPEN;
  64.         return -1;
  65.     }
  66.  
  67.     /* check locks */
  68.     if (!(btp->flags & BTLOCKS)) {
  69.         errno = BTELOCK;
  70.         return -1;
  71.     }
  72.  
  73.     /* set cursor to first key */
  74.     btp->cbtpos.node = btp->bthdr.first;
  75.     btp->cbtpos.key = 1;
  76.  
  77.     /* check if tree is empty */
  78.     if (btp->cbtpos.node == NIL) {
  79.         btp->cbtpos.key = 0;
  80.         bt_ndinit(btp, btp->cbtnp);
  81.         errno = BTENKEY;
  82.         return -1;
  83.     }
  84.  
  85.     /* read current node */
  86.     if (bt_ndget(btp, btp->cbtpos.node, btp->cbtnp) == -1) {
  87.         BTEPRINT;
  88.         terrno = errno;
  89.         btp->cbtpos.node = NIL;
  90.         btp->cbtpos.key = 0;
  91.         bt_ndinit(btp, btp->cbtnp);
  92.         errno = terrno;
  93.         return -1;
  94.     }
  95.  
  96.     return 0;
  97. }
  98.